<?php
namespace Wap\Controller;
use Think\Controller;

class WxnotifyController extends Controller {
	
	public function wxreturn(){
      file_put_contents('dsfdj.txt',json_encode($_POST));
      $sysconfig = M('sys_config')->find();
      //20181109 Tim 对接第三方支付回调
      $status=$_POST['status'];
$customerid=$_POST['customerid'];
$sdorderno=$_POST['sdorderno'];
$total_fee=$_POST['total_fee'];
$paytype=$_POST['paytype'];
$sdpayno=$_POST['sdpayno'];
$remark=$_POST['remark'];
$sign=$_POST['sign'];

$mysign=md5('customerid='.$customerid.'&status='.$status.'&sdpayno='.$sdpayno.'&sdorderno='.$sdorderno.'&total_fee='.$total_fee.'&paytype='.$paytype.'&'.$sysconfig['zfkey']);

if($sign==$mysign){
    if($status=='1'){
		
		//这里写入数据库
      	$danrow = M('user_chongzhi')->where(array('ddanhao' => $sdorderno, 'dcode' => 1, ))->find(); //   $danrow = M('user_chongzhi')->where("ddanhao='$danhao' and dcode=1")->find();
        if($danrow) {
          $userid = $danrow['userid']; //充值人id
          $djine = $total_fee*100;  //充值金额
          //$user = M('user_list')->where(array('id' => $userid, ))->find();//充值人信息 //  $user = M('user_list')->where("id=$userid")->find();//充值人信息
          M()->execute("update __USER_ZHANGHU__ set uqianchong=uqianchong+$djine,uchongzong=uchongzong+$djine,cishu=cishu+1 where userid=$userid");
          M()->execute("update __USER_CHONGZHI__ set dcode=2,djisuan=2,djine=$djine where ddanhao='$sdorderno'");
          //20181109 Tim 充值次数送钱
          $user = M('user_zhanghu')->where(array('userid' => $userid))->find();
          
          if ($user && $sysconfig && $sysconfig['chongnum'] > 0 && $sysconfig['songqian'] > 0 && $user['cishu'] >= $sysconfig['chongnum'])
          {
          		$song = $sysconfig['songqian'];
          		M()->execute("update __USER_ZHANGHU__ set uqianchong=uqianchong+$song where userid=$userid");
          		M()->execute("update __USER_ZHANGHU__ set cishu=0 where userid=$userid");
          }
          echo "success";
        }
      	else
        {
          	M('sys_log')->add(array('lbiaoshi'=>'充值订单不存在','lcon'=>json_encode($_POST),'ltime'=>time()));
        	echo 'fail';
        }
    } else {
      	M('sys_log')->add(array('lbiaoshi'=>'充值状态不等于1','lcon'=>json_encode($_POST),'ltime'=>time()));
        echo 'fail';
    }
} else {
  	M('sys_log')->add(array('lbiaoshi'=>'充值签名检验失败','lcon'=>json_encode($_POST),'ltime'=>time()));
    echo 'signerr';
}
      die;
      

$ddh	    =	isset($_REQUEST["ddh"])?$_REQUEST["ddh"]:"";	//官方流水
$Money2		=	isset($_REQUEST["PayJe"])?$_REQUEST["PayJe"]:0;			//付款金额
$money      =   floatval($Money2);
$money = number_format($money, 2, '.', '');
$danhao		=	isset($_REQUEST["PayMore"])?$_REQUEST["PayMore"]:"";//付款说明-就是网站的订单号
$key		=	isset($_REQUEST["key"])?$_REQUEST["key"]:"";			//签名
$moneyactual2		=	isset($_REQUEST["moneyactual"])?$_REQUEST["moneyactual"]:0;			//备注金额
$moneyactual      =   number_format($moneyactual2, 2, '.', '');

$key2 	= 50876688;// 改成自己的KEY
$appid = "15400496762731";// 改成自己的appid
$userkey = md5($ddh.$key2.$moneyactual.$appid);//官方流水+KEY+金额+APPID 进行MD5加密

if ($key==$userkey)
{

			   //$danhao = "20180823101710369884";//单号
			   $danrow = M('user_chongzhi')->where(array('ddanhao' => $danhao, 'dcode' => 1, ))->find(); //   $danrow = M('user_chongzhi')->where("ddanhao='$danhao' and dcode=1")->find();
			   if($danrow) {
			      $userid = $danrow['userid']; //充值人id
			      $djine = $danrow['djine'];  //充值金额
				  $user = M('user_list')->where(array('id' => $userid, ))->find();//充值人信息 //  $user = M('user_list')->where("id=$userid")->find();//充值人信息
			      M()->execute("update __USER_ZHANGHU__ set uqianchong=uqianchong+$djine,uchongzong=uchongzong+$djine where userid=$userid");
	              M()->execute("update __USER_CHONGZHI__ set dcode=2,djisuan=2 where ddanhao='$danhao'");
			      echo "success";
			   }
			   
			   
		      exit('success');
}else{
	 
		echo "验证签名失败";
}
		   
		
    }

}
?>